-- NewsPaperRender11
-- 对应样式 1*1

local ClassRef = app.mvc.createRenderClass(...)

-- 不同行数的标题对应的不同图片（key=行数）
local LineToPic = {[2] = "P_Grid1x1_2T", [3] = "P_Grid1x1_3T"}

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewsPaperRender_11))
    sprite:init()
    return sprite
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.text:setVisible(false)
    self._ui.title:setVisible(false)

    self._tltWidth, self._tltHeight = self._ui["Panel/text/tlt_1"]:getContentSize2()
    self._ui["Panel/text/tlt_1"]:setTextAreaSize(cc.size(self._tltWidth, 0))
    self._ui["Panel/title/tlt_1"]:setTextAreaSize(cc.size(self._tltWidth, 0))
    self._ui["Panel/text/tlt_1"]:getVirtualRenderer():setLineBreakWithoutSpace(true)    
    self._ui["Panel/title/tlt_1"]:getVirtualRenderer():setLineBreakWithoutSpace(true) 

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local data = self._data
        local eventId = data.eventId
        local playerInfo = data.newsInfo or {}
        local extraStr = (playerInfo[1] and playerInfo[1].BaseInfo) and playerInfo[1].BaseInfo.name or "" -- 目前只需要玩家名字
        local conf = GD:getNewsPaperEventById(eventId)
        if not conf or not conf.ui11 then return end
        if conf.ui11 == "text" then
            self._ui.text:setVisible(true)
            local imageNode12 = self._ui["Panel/text/Image_12"]
            local imageNode13 = self._ui["Panel/text/Image_13"]
            local tltNode = self._ui["Panel/text/tlt_1"]
            local titleStr = conf.describe and "    "..conf.title1 or ""..conf.title1           
            tltNode:setString(string.format(L(titleStr), extraStr))  
            tltNode:getVirtualRenderer():setLineSpacing(-5)              
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic11)
            local tltVirtucal = tltNode:getVirtualRendererSize()
            tltNode:setContentSize(tltVirtucal)
            if tltVirtucal.height - self._tltHeight*2 >= self._tltHeight/2 then
                imageNode12:setVisible(false)
                imageNode13:setVisible(true)
                bgPath = string.format("%s%s.png",Res.newsPaperPath, LineToPic[3])
                imageNode13:loadTexture(bgPath)
            else
                imageNode12:setVisible(true)
                imageNode13:setVisible(false)
                bgPath = string.format("%s%s.png",Res.newsPaperPath, LineToPic[2])
                imageNode12:loadTexture(bgPath)
            end
            if conf.describe then
                self._ui.Button_Check:setVisible(true)
                self._ui.Button_Check:setTouchEnabled(true)
                self._ui.Button_Check:addTouchEventListener(function(sender, eventType)
                    if eventType == ccui.TouchEventType.ended then
                        executeFunc(self._onOperate, "openDetailsView", data)
                    end
                end)
            end
        elseif conf.ui11 == "title" then
            self._ui.title:setVisible(true)
            self._ui["Panel/title/tlt_1"]:setString(string.format(L(conf.title1), extraStr))
            self._ui["Panel/title/tlt_1"]:getVirtualRenderer():setLineSpacing(-5)              
            local tltVirtucal = self._ui["Panel/title/tlt_1"]:getVirtualRendererSize()
            self._ui["Panel/title/tlt_1"]:setContentSize(tltVirtucal)
            local bgPath = string.format("%s%s.png",Res.newsPaperPath,conf.pic11)
            self._ui["Panel/title/bg"]:loadTexture(bgPath)
        end
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef